--
-- Author: shunguo.chen
-- Date: 2018-03-16 13:48:53
--
-- ChessActionTipRender 谜棋action提示

local ClassRef = app.mvc.createRenderClass(...)

function ClassRef.createWithData(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.ChessActionTipNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

-- data = {msg=, icon=}
function ClassRef:init(data)
	self.super.init(self, data)

	display.setCascadeOpacityEnabled(self, true)

	self:setTouchEnabled(false)
	self:setEnabled(false)

	self._defaults = {
		KEY_FONT_SIZE = self._ui.Text:getFontSize(),
		-- KEY_FONT_FACE = Res.font,
		KEY_FONT_COLOR_STRING = display.stringWithColor(self._ui.Text:getTextColor())
	}

	self._pos = cc.p(self._ui.Text:getPosition())
	self._ui.Text:removeFromParent()

	self:refresh()
end

function ClassRef:refresh()
	if self._data then
		self._ui.icon:loadTexture(self._data.icon)
		self:setText(self._data.msg)
	end
end

function ClassRef:setText(msg)
	if self._richText then
		self._richText:removeFromParent()
	end
	
	local richText = ccui.RichText:createWithXML(msg, self._defaults)
	richText:setTouchEnabled(false)
	richText:setEnabled(false)
	richText:setAnchorPoint(cc.p(1, 0.5))
	richText:setContentSize(cc.size(100, 0))
	richText:setPosition(self._pos.x, self._pos.y)
	display.setCascadeOpacityEnabled(richText, true)
	self._ui.Panel:addChild(richText)
	self._richText = richText
end

-- fadeType = <0	fadeIn
-- fadeType = 0		fadeOut
-- fadeType = >0	无fade
function ClassRef:playMove(fadeType, cb)
	local time = 1
	local move = cca.moveBy(time, 0, 42)
	local act = move
	if fadeType < 0 then
		local fade = cca.fadeIn(time)
		act = cca.spawn({fade, move})
	elseif fadeType == 0 then
		local fade = cca.fadeOut(time)
		act = cca.spawn({fade, move})
	end
	if cb then
		local func = cca.cb(function() executeFunc(cb) end)
		act = cca.seq({act, func})
	end
	self:runAction(act)
end

return ClassRef